<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in types" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="showtree=true" />

        <Tree :is-show.sync="showtree" class="treeBox" @updateDept="updateDept" />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="uploadForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import hireType from '@/api/constant/employees.js'
import { getDepartments } from '@/api/departments.js'
import { tranListToTreeData, parseTime } from '@/utils/index.js'
import { addEmployee } from '@/api/employee.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 树形结构数据
      showtree: false,

      // 雇佣形式
      types: hireType.hireType,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getDepartmentsFN()
  },
  methods: {
    updateDept(dept) {
      this.formData.departmentName = dept
    },
    closeDialog() {
      this.$emit('update:showDialog', false)
      // 清空内容
      this.$refs.addForm.resetFields()
      // 清空未校验的选项
      this.formData.correctionTime = ''
    },
    // 获取树形结构的内容
    async getDepartmentsFN() {
      const res = await getDepartments()
      // 机构格式化
      this.treeData = tranListToTreeData(res.depts)
    },

    // 点击上传表单
    uploadForm() {
      this.$refs.addForm.validate(async(isok) => {
        if (isok) {
          // 格式化时间
          this.formData.correctionTime = parseTime(this.formData.correctionTime, '{y}-{m}-{d}')
          this.formData.timeOfEntry = parseTime(this.formData.timeOfEntry, '{y}-{m}-{d}')
          // 调接口新增内容
          await addEmployee(this.formData)
          this.$message.success('添加成功！')
          // 关闭弹窗
          this.closeDialog()
          this.$parent.getEmployeeLists()
        }
      })
      console.log(123)
    }
  }
}
</script>

<style lang="scss" scoped>
  .treeBox{
    width: 240px;
    border: 1px solid #eee;
  }
</style>
